#include "Segmentation.h"

void doSegmentation()
{
	cout<<"Doing image segmentation..."<<endl;
	IplImage* grayImage = cvCreateImage(cvGetSize(rawImage), IPL_DEPTH_8U, 1);
	IplImage* binImage = cvCreateImage(cvGetSize(grayImage), IPL_DEPTH_8U, 1);

	cout<<"Removing outer blank..."<<endl;
	removeOuterBlank( rawImage );

	cout<<"Converting image into gray level image..."<<endl;
	convertToGray( rawImage, grayImage);

	cout<<"Smoothing image..."<<endl;
	cvSmooth(grayImage, grayImage, CV_GAUSSIAN, 5);

	cout<<"Enhancing branches..."<<endl;
	enhanceGrayImage(grayImage);

	cout<<"Converting image into binary image..."<<endl;
	convertToBinary( grayImage, binImage, THRESHOLD);

	//cout<<"Searching for largest connected component..."<<endl;
	//if (*(binImage->imageData + CENTERX*binImage->widthStep+CENTERY) == 0){
	//	cout<<"Initial position for searching is wrong,"
	//		<<"Please select another and try again."<<endl;
	//	exit(0);
	//}
	cvFloodFill(binImage, cvPoint(CENTERX, CENTERY), cvScalar(120), 
		cvScalar(5), cvScalar(5),NULL, 4, NULL);
	copyImage(binImage,binaryImage, 120);
	cvReleaseImage(&binImage);
	cout<<"Image segmentation completed."<<endl;
}